package com.soywer.rpc.future;


import java.util.concurrent.*;


/**
 * 功能描述：
 *
 * @Author: limeng54
 * @Date: 2021/4/2 15:06
 */
public class FutureDemo2 {
    public static void main(String[] args) throws InterruptedException, ExecutionException {

        long start = System.currentTimeMillis();

        FutureTask<String> f1 = new FutureTask<>(new MyCallable("包子",1));
        FutureTask<String> f2 = new FutureTask<>(new MyCallable("黄瓜",2));
        FutureTask<String> f3 = new FutureTask<>(new MyCallable("牛肉",3));

        new Thread(f1).start();
        new Thread(f2).start();
        new Thread(f3).start();

        System.out.println("包子是否完成:"+f1.isDone());
        System.out.println("黄瓜是否完成:"+f2.isDone());
        System.out.println("牛肉是否完成:"+f3.isDone());

        System.out.println(f1.get());
        System.out.println(f2.get());
        System.out.println(f3.get());

        System.out.println("包子是否完成:"+f1.isDone());
        System.out.println("黄瓜是否完成:"+f2.isDone());
        System.out.println("牛肉是否完成:"+f3.isDone());

        long end = System.currentTimeMillis();
        System.out.println("准备完毕时间："+(end-start));
    }


    static class MyCallable implements Callable<String> {

        private String menuName;
        private Integer delayMinute;

        public MyCallable(String menuName,Integer delayMinute) {
            this.menuName = menuName;
            this.delayMinute = delayMinute;
        }

        @Override
        public String call() throws Exception {

            try {
                Thread.sleep(1000*this.delayMinute);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return this.menuName + "准备完毕";
        }
    }

//        long start = System.currentTimeMillis();
//
//        // 等凉菜
//        Callable ca1 = new Callable(){
//
//            @Override
//            public String call() throws Exception {
//                try {
//                    Thread.sleep(1000);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//                return "凉菜准备完毕";
//            }
//        };
//        FutureTask<String> ft1 = new FutureTask<String>(ca1);
//
//        new Thread(ft1).start();
//
//        // 等包子 -- 必须要等待返回的结果，所以要调用join方法
//        Callable ca2 = new Callable(){
//
//            @Override
//            public Object call() throws Exception {
//                try {
//                    Thread.sleep(1000*3);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//                return "包子准备完毕";
//            }
//        };
//        FutureTask<String> ft2 = new FutureTask<String>(ca2);
//        new Thread(ft2).start();
//
//
//        FutureTask<String> ft3 = new FutureTask<>(
//                new Callable<String>() {
//                    @Override
//                    public String call() throws Exception {
//                        try {
//                            Thread.sleep(1000*4);
//                        } catch (InterruptedException e) {
//                            e.printStackTrace();
//                        }
//                        return "牛肉准备完毕";
//                    }
//                }
//        );
//
//        new Thread(ft3).start();
//        System.out.println("牛肉是否完成:"+ft3.isDone());
//        System.out.println("包子是否完成:"+ft2.isDone());
//        System.out.println("凉菜是否完成:"+ft1.isDone());
//
//
//        System.out.println(ft2.get());
//        System.out.println(ft1.get());
//        System.out.println(ft3.get());
//
//        System.out.println("包子是否完成:"+ft3.isDone());
//        System.out.println("包子是否完成:"+ft2.isDone());
//        System.out.println("凉菜是否完成:"+ft1.isDone());
//
//        long end = System.currentTimeMillis();
//        System.out.println("准备完毕时间："+(end-start));
}
